var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp();

Page({
  data: {
    res: {},
    page: 1,
    size: 10,
    totalPages: 1,
    radioItems: [
      { name: '唤醒身体', conten: '30分钟', value: '30', money: 10, checked: true, color: '#43cbc9' },
      { name: '通行气血', conten: '60分钟', value: '60', checked: false, money: 20, color: '#fff'},
      { name: '舒张胫骨', conten: '90钟', value: '90', money: 30, checked: false, color: '#fff' },
      { name: '舒张胫骨', conten: '120分钟', value: '120', money: 40, checked: false, color: '#fff' }
    ],
    bodylist: [
      { name: '肩部', value: '0', checked: true, color: '#43cbc9' },
      { name: '胸部', value: '1', checked: false, color: '#fff' },
      { name: '背部', value: '2', checked: false, color: '#fff' },
      { name: '臀部', value: '3', checked: false, color: '#fff' },
      { name: '膝盖部', value: '4', checked: false, color: '#fff' }
    ],
    money:10
  },
  onLoad: function (options) {
    var that = this;
    // 获取传递过来的热疗仪数据
    if (options.query) {
      let res = JSON.parse(options.query);
      that.setData({
        res: res
      });
    }
  },
  //改变按摩部位
  bodyChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.currentTarget.dataset.value);

    var bodylist = this.data.bodylist;
    for (var i = 0, len = bodylist.length; i < len; ++i) {
      bodylist[i].color = "#fff";
      bodylist[i].checked = bodylist[i].value == e.currentTarget.dataset.value;
      if (bodylist[i].checked) {
        bodylist[i].color = "#43cbc9";
      }
    }

    this.setData({
      bodylist: bodylist
    });
  },
  //条款同意
  bindAgreeChange: function (e) {
    this.setData({
      isAgree: !!e.detail.value.length
    });
  },

  //改变按摩套餐
  radioChange: function (e) {
    console.log("当前服务类型是："+e.currentTarget.dataset.value);
    var that = this
    that.pircese(e.currentTarget.dataset.value)
  },
  pircese: function (x) {
    console.log("当前服务类型x是：" + x);
    var that = this;
    var checkboxItems = that.data.radioItems;
    if (!checkboxItems[x].checked) {
      for (var i = 0; i < checkboxItems.length; i++) {
        if (i == x) {
          checkboxItems[i].checked = true
          checkboxItems[i].color = '#43cbc9'
          console.log("当前服务费用是:"+checkboxItems[i].money);
          that.setData({
            money: checkboxItems[i].money
          });
        } else {
          checkboxItems[i].checked = false
          checkboxItems[i].color = '#fff'
        }
      }
    }
    that.setData({
      radioItems: checkboxItems
    });
  },




  onReachBottom() {
    
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },
  // radioChange: function (e) {
  //   console.log('radio发生change事件，携带value值为：', e.detail.value);

  //   var radioItems = this.data.radioItems;
  //   for (var i = 0, len = radioItems.length; i < len; ++i) {
  //     radioItems[i].checked = radioItems[i].value == e.detail.value;
  //   }

  //   this.setData({
  //     radioItems: radioItems
  //   });
  // },
  submit: function(){
    var this_ = this;
    var res = this_.data.res;
    // 获取选中的时间和金额
    var radioItems = this_.data.radioItems;
    // 获取选中的按摩类型
    var bodylist = this_.data.bodylist;
    var orderSales = 0;
    var serviceType = "";
    var serviceTime = 10;
    console.log(this_.data.res);
    // 创建订单
    console.log("开始创建订单");
    console.log(this.data.radioItems);
    for (var i = 0; i < radioItems.length; i++){
      if (radioItems[i].checked){
        orderSales = radioItems[i].money;
        serviceTime = radioItems[i].value;
      }
    }
    for (var i = 0; i < bodylist.length; i++) {
      if (bodylist[i].checked) {
        serviceType = bodylist[i].value;
      }
    }

    
    console.log(orderSales);
    util.request(api.OrderDevcieSubmit, {
     deviceId: res.id,
     orderSales: orderSales,
     userType:'JS',
     buyType:2,
     serviceType: serviceType,
     serviceTime: serviceTime
     }, "POST").then(function (res) {
       console.log("订单创建成功");
       console.log(res);
       // 调用微信付款接口
       this_.payOrder(res);
      //付款成功，改变订单状态,实际付款金额
       //this_.updateOrder(res, orderSales);
     });
  },
  updateOrder: function (res, orderSales){
    util.request(api.OrderDevcieUpdate, {
      id: res.id,
      actualPayment: orderSales,
      status:1,
      payType:1
    //payDate:"2018-08-31"
    }, "POST").then(function (res) {
      console.log("订单修改成功,付款成功");
      console.log(res);
      wx.navigateTo({
        url: "/pages/deviceOrderMsg/deviceOrderMsg?code=" + res.code
      });
    });
  },
   // “去付款”按钮点击效果
  payOrder: function (obj) {
    let that = this;
      console.log(obj);
    if (obj != null) {
        const payParam = obj;
        console.log("支付过程开始");
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res) {
            console.log("支付过程成功");
            console.log("支付成功相应：" + JSON.stringify(res));
            //付款成功，改变订单状态,实际付款金额
            //that.updateOrder(res);
            wx.navigateTo({
              url: "/pages/deviceOrderMsg/deviceOrderMsg?code=" + obj.result.code
            });
          },
          'fail': function (res) {
            console.log("支付过程失败");
            console.log("支付过程失败:"+JSON.stringify(res));
            util.showErrorToast('支付失败');
          },
          'complete': function (res) {
            console.log("支付过程结束")
            console.log("支付过程结束" + JSON.stringify(res))
          }
        });
      } else {
        util.showErrorToast('订单状态已变更，请刷新后再进行操作');
      }
  }
})
